/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.event.listener;

import vip.dexian.admin.entity.Admin;
import vip.dexian.admin.event.AdminEnableDisableEvent;
import vip.dexian.admin.service.OnlineAdminService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 敏感操作日志记录事件监听
 *
 * @author 挺好的 2023年06月08日 9:27
 */
@Component
@Slf4j
public class AdminSensitiveLogListener {

    /**
     * 在线用户
     */
    @Resource (name = "onlineAdminServiceImpl")
    private OnlineAdminService onlineAdminService;

    /**
     * 启用异步发布
     *
     * @param event
     *         启用禁用事件
     */
    @EventListener
    @Async
    public void afterEnableDisable (AdminEnableDisableEvent event) {
        log.debug("启用/禁用事件");
        Admin admin = event.getData();

        // 保存日志->->
        // 踢出当前用户
        this.onlineAdminService.logoutByUsername(admin.getUsername());
    }
}
